package cn.gailvlun.gll.config

import android.arch.lifecycle.MutableLiveData
import android.content.Context
import android.view.View
import cn.gailvlun.gll.presentation.charge.model.Outcome


fun Int.dp(context: Context): Int = context.dip(this.toFloat())
fun Float.dp(context: Context): Int = context.dip(this)
fun Context.dip(dp: Float): Int = (resources.displayMetrics.density * dp).toInt()

fun View.dip(dp: Float) = context.dip(dp)

fun <T> MutableLiveData<Outcome<T>>.failed(msg: String) {
    with(this) {
        loading(false)
        value = Outcome.failure(msg)
    }
}

fun <T> MutableLiveData<Outcome<T>>.success(data: T) {
    with(this) {
        loading(false)
        value = Outcome.success(data)
    }
}

fun <T> MutableLiveData<Outcome<T>>.loading(isLoading: Boolean) {
    with(this) {
        value = Outcome.loading(isLoading)
    }
}